# -*- encoding: utf-8 -*-

import datetime
from functools import wraps


def check_id_no(func):
    @wraps(func)
    def decorator(*args, **kwargs):
        if not args[0]:
            return False
        if len(args[0]) not in [15, 18]:
            return False
        if not isinstance(args[0], str):
            return False
        return func(*args, **kwargs)

    return decorator


def check_address_code(address_code, info: dict) -> bool:
    if address_code[0] == '8' and not info['province']:
        return False
    if address_code[0] != '8' and (not info['province'] or not info['district']):
        return False

    return True


def check_birthday_code(birthday_code: str) -> bool:
    birthday_code = str(birthday_code)
    if not birthday_code or len(birthday_code) != 8:
        return False
    try:
        year = int(birthday_code[0:4])
        month = int(birthday_code[4:6])
        day = int(birthday_code[6:8])
        if not check_date(year, month, day):
            return False
    except ValueError:
        return False
    return True


def check_date(year: int, month: int, day: int) -> bool:
    year, month, day = int(year), int(month), int(day)
    if not year or year < 1800 or year > datetime.datetime.now().year:
        return False
    if not month or not (1 <= month <= 12):
        return False
    if not day or not (1 <= day <= 31):
        return False
    try:
        datetime.date(year, month, day)
        return True
    except ValueError:
        return False


def check_order_no(order_no: str) -> bool:
    return len(order_no) == 3
